package com.example.day44.wm;

import android.content.Context;
import android.view.Gravity;
import android.view.WindowManager;

import com.example.day44.widget.FloatView;


/**
 * 注意 实例化 WindowManager的时候
 * 如果想程序运行在后台也能显示这个悬浮窗口 ,实例化的时候 使用应用程序的全局上下文
 * 如果只想在应用程序中使用的话,使用Activity的上下文,  WindowManager.LayoutParams.TYPE_APPLICATION;
 */
public class WmHelper {
    private static WmHelper helper;
    private WindowManager wm;
    private FloatView floatView;
    private WindowManager.LayoutParams params;


    private static Context context;

    public static void init(Context context) {
        WmHelper.context = context;
    }

    private WmHelper() {
    }

    public static WmHelper getInstance() {
        if (helper == null) {
            helper = new WmHelper();
        }
        return helper;
    }


    public void show() {
        floatView = new FloatView(context);
        /**
         * int w, 显示的宽度
         * int h, 显示的高度
         * int xpos,
         * int ypos,
         * int _type,
         int _flags,
         int _format
         */
        if (params == null) {
            params = new WindowManager.LayoutParams();
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.x = 400;
            params.y = 500;
            //设置弹出窗口的显示级别
            /**
             * type
             * 1 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT  类型与关机界面的显示级别
             * 2 WindowManager.LayoutParams.TYPE_PHONE  类型与类似打电话界面的显示级别
             * 3.WindowManager.LayoutParams.TYPE_PHONETYPE_APPLICATION  只能在应用程序中显示
             */
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
            //不优先获得焦点
            params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            //设置windomanager的透明度
            params.alpha = 1.0f;
            params.gravity = Gravity.LEFT | Gravity.TOP;
        }
        floatView.setParams(params);
        getWm().addView(floatView, params);
    }

    public WindowManager getWm() {
        if (wm == null) {
            wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return wm;
    }

    public void remove() {
        if (floatView != null) {
            getWm().removeView(floatView);
        }
    }

    public WindowManager.LayoutParams getParams() {
        return params;
    }

    public void setParams(WindowManager.LayoutParams params) {
        this.params = params;
    }
}
